package org.happen.android.widget;

import java.util.ArrayList;
import java.util.List;

import org.happen.android.base.BaseLvAdapter;
import org.liushui.mycommons.android.annotation.ViewInject;
import org.liushui.mycommons.android.annotation.helper.InjectHelper;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import org.happen.android.R;

/**
 * 选择对话框
 */
public class SelectDialog implements View.OnClickListener
{

	View contentView;

	@ViewInject(id = R.id.btn_dialog_cancel)
	Button btnCancnel;
	@ViewInject(id = R.id.listview)
	ListView listView;

	Context context;
	Dialog dialog;

	OnItemSelectCallback onItemSelectCallback = new OnItemSelectCallback() {

		public void onItemSelect(SelectDialog dialog, int position)
		{

		}

		public void onCancel(SelectDialog dialog)
		{

		}
	};

	String cancelText;
	boolean isFirstTitle;
	List<String> data = new ArrayList<String>();

	LvAdapter adapter;

	public SelectDialog(Context context)
	{
		this.context = context;
	}

	public Dialog create()
	{
		contentView = View.inflate(context, R.layout.dialog_selectl, null);
		InjectHelper.init(this, contentView);

		btnCancnel.setOnClickListener(this);
		if (cancelText != null)
		{
			btnCancnel.setText(cancelText);
		}
		adapter = new LvAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(adapter);

		dialog = new Dialog(context, R.style.AppTheme_Dialog_SELECT);
		dialog.setContentView(contentView);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
		lp.windowAnimations = android.R.style.Animation_InputMethod;
		window.setAttributes(lp);
		return dialog;
	}

	public void setItems(String[] items)
	{
		setItems(items, false);
	}

	public void setItems(String[] items, boolean isFirstTitle)
	{
		this.isFirstTitle = isFirstTitle;
		data.clear();
		for (String i : items)
		{
			data.add(i);
		}
	}

	public List<String> getItems()
	{
		return data;
	}

	public String getItem(int position)
	{
		return data.get(position);
	}

	/**
	 * 设置按钮显示，使用null，则显示默认(者取消 )
	 * 
	 * @ReqParam cancel
	 */
	public void setCancelButtonText(String cancel)
	{
		this.cancelText = cancel;
	}

	public void onClick(View v)
	{
		if (v == btnCancnel)
		{
			onItemSelectCallback.onCancel(this);
			dialog.dismiss();
		}
	}

	public Dialog getDialog()
	{
		return dialog;
	}

	public OnItemSelectCallback getOnItemSelectCallback()
	{
		return onItemSelectCallback;
	}

	public void setOnItemSelectCallback(OnItemSelectCallback onItemSelectCallback)
	{
		this.onItemSelectCallback = onItemSelectCallback;
	}

	class LvAdapter extends BaseLvAdapter
	{

		public int getCount()
		{
			return data.size();
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			TextView tv = null;
			if (convertView == null)
			{
				convertView = View.inflate(context, R.layout.item_dialog_select_lv, null);
			}
			tv = (TextView) convertView;
			tv.setText(data.get(position));
			if (data.size() < 2)
			{
				tv.setBackgroundResource(R.drawable.list_item_select_bg_all);
			} else if (position == 0)
			{
				if (isFirstTitle)
				{
					tv.setBackgroundColor(context.getResources().getColor(R.color.transparent));
				} else
				{
					tv.setBackgroundResource(R.drawable.list_item_select_bg_top);
				}
			} else if (position == (data.size() - 1))
			{
				tv.setBackgroundResource(R.drawable.list_item_select_bg_bottom);
			} else
			{
				tv.setBackgroundResource(R.drawable.list_item_select_bg);
			}
			return tv;
		}

		public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
		{
			if (position == 0)
			{
				if (!isFirstTitle)
				{
					onItemSelectCallback.onItemSelect(SelectDialog.this, position);
				}
			} else
			{
				onItemSelectCallback.onItemSelect(SelectDialog.this, position);
			}
		}
	}

	public interface OnItemSelectCallback
	{

		void onItemSelect(SelectDialog dialog, int position);

		void onCancel(SelectDialog dialog);
	}
}